package com.ztx.web.controller.caiwu;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztx.model.QkInfo;
import com.ztx.model.QkInfoSum;
import com.ztx.model.QkInfoTemp;
import com.ztx.pagemodel.SessionInfo;
import com.ztx.pagemodel.easyui.DataGrid;
import com.ztx.pagemodel.easyui.Json;
import com.ztx.pagemodel.easyui.PageHelper;
import com.ztx.service.base.IBaseService;
import com.ztx.service.caiwu.IQkService;
import com.ztx.utils.Constants;

@Controller
@RequestMapping("qkController")
public class QkSettleController {

	private static final Logger log = LoggerFactory
			.getLogger(QkSettleController.class);
	@Resource
	private IQkService qkService;

	@Resource
	private IBaseService baseService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String audit() {
		return "caiwu/qk/qkinfo";
	}

	/**
	 * 查询欠款数据列表
	 * 
	 * @param ph
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "datagrid", method = RequestMethod.POST)
	@ResponseBody
	public DataGrid<QkInfoSum> datagrid(PageHelper ph, QkInfo params) {
		return qkService.getqkList(ph, params);
	}

	/**
	 * 直接审核通过
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "auditpass/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Json auditpass(@PathVariable(value = "id") long id,
			HttpServletRequest request) {
		SessionInfo session = (SessionInfo) request.getSession().getAttribute(
				Constants.SESSIONINFO);
		Json json = new Json();
		try {
			qkService.doAuditPass(id, session);
			json.setMsg("操作成功！");
		} catch (Exception e) {
			log.error("欠款结算:", e);
			json.setSuccess(false);
			json.setMsg(e.getMessage());
		}
		
		return json;
	}

	/**
	 * 欠款结算，财务输入结算金额和结算日期
	 * 
	 * @param qkinfo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "settle", method = RequestMethod.POST)
	@ResponseBody
	public Json settle(QkInfoSum qkinfo, HttpServletRequest request) {
		SessionInfo session = (SessionInfo) request.getSession().getAttribute(
				Constants.SESSIONINFO);
		Json json = new Json();
		try {
			qkService.settle(qkinfo, session);
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}

	/**
	 * 修改后审核通过
	 * 
	 * @param info
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	@ResponseBody
	public Json editAndPass(QkInfoTemp info, HttpServletRequest request) {
		SessionInfo session = (SessionInfo) request.getSession().getAttribute(
				Constants.SESSIONINFO);
		Json json = new Json();
		try {
			qkService.editAndPass(info, session);
			json.setMsg("操作成功！");
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}

	/**
	 * 将report_all表中数据进行分类,导入到t_qk_info (欠款信息表)
	 * 
	 * @return
	 */
	@RequestMapping(value = "synData", method = RequestMethod.POST)
	@ResponseBody
	public Json synData() {
		Json json = new Json();
		try {
			qkService.doSynData();
			json.setMsg("操作成功！");
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}

}
